//
//  LHNewFeatureViewController.m
//  OCSinaWeibo
//
//  Created by LiHong on 15/11/3.
//  Copyright © 2015年 LiHong. All rights reserved.
//

#import "LHNewFeatureViewController.h"
#import "Masonry.h"
#define itemCount 4
@interface LHNewFeatureViewController ()

@property (nonatomic,strong)UICollectionViewFlowLayout *layout;

@end

@implementation LHNewFeatureViewController

static NSString * const reuseIdentifier = @"Cell";
//static NSInteger const itemCount = 4;
- (instancetype)init{
    if (self = [super initWithCollectionViewLayout:self.layout]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[LHNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self prepareUI];
}

- (void)prepareUI{
    self.layout.itemSize = [UIScreen mainScreen].bounds.size;
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    // 水平滚动
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return itemCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LHNewFeatureCell *cell = (LHNewFeatureCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
   // LHNewFeatureCell *cell = (LHNewFeatureCell *)[collectionView cellForItemAtIndexPath:indexPath];
    // Configure the cell
  //  cell.backgroundColor = [UIColor randomColor];
    cell.imageIndex = indexPath.item;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath *showIndexPath = [collectionView indexPathsForVisibleItems].firstObject;
    
    cell = (LHNewFeatureCell *)[collectionView cellForItemAtIndexPath:showIndexPath];
    
    if (showIndexPath.item == itemCount - 1) {
        [self startButtonAnimationWithCell:cell];
    }
}

- (void)startButtonAnimationWithCell:(LHNewFeatureCell *)cell{
    cell.startButton.hidden = NO;
    cell.startButton.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:1 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:UIViewAnimationOptionTransitionNone animations:^{
        cell.startButton.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - 懒加载
- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
    }
    return _layout;
}
@end

#pragma mark - 自定义Cell
@implementation LHNewFeatureCell
// 从代码创建会调用此方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}

//- (void)startButtonAnimation{
//    self.startButton.hidden = NO;
//    self.startButton.transform = CGAffineTransformMakeScale(0, 0);
//    [UIView animateWithDuration:1 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:UIViewAnimationOptionTransitionNone animations:^{
//        self.startButton.transform = CGAffineTransformIdentity;
//    } completion:^(BOOL finished) {
//        
//    }];
//}


// 准备UI
- (void)prepareUI{
    [self.contentView addSubview:self.backgroundImageView];
    [self.contentView addSubview:self.startButton];
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-160);
    }];
    
}

- (void)setImageIndex:(NSInteger)imageIndex{
    _imageIndex = imageIndex;
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%zd",imageIndex + 1];
    self.backgroundImageView.image = [UIImage imageNamed:imageName];
    self.startButton.hidden = YES;
}


- (void)startButtonClick{
    NSLog(@"startButtonClick");
}

#pragma mark - 懒加载
/// 背景
- (UIImageView *)backgroundImageView{
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc]init];
        _backgroundImageView.image = [UIImage imageNamed:@"new_feature_1"];
    }
    return _backgroundImageView;
}
/// 开始体验按钮
- (UIButton *)startButton{
    if (!_startButton) {
        _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
        [_startButton setTitle:@"开始体验" forState:UIControlStateNormal];
        [_startButton addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
       // [_startButton sizeToFit];
    }
    return _startButton;
}

@end